﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DGIS.Map.Models;
using DGIS.Model;

namespace DGIS.Map.Templetes
{
    /// <summary>
    /// ProgressTemplete.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressTemplete : UserControl
    {
        public ProgressTemplete()
        {
            InitializeComponent();
        }

        #region 附加属性

        public ImageSource IconSource
        {
            get { return (ImageSource)GetValue(IconSourceProperty); }
            set { SetValue(IconSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconSourceProperty =
            DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(ProgressTemplete), new PropertyMetadata(null));



        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(ProgressTemplete), new PropertyMetadata("未知"));



        public double FileSize
        {
            get { return (double)GetValue(FileSizeProperty); }
            set { SetValue(FileSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileSizeProperty =
            DependencyProperty.Register("FileSize", typeof(double), typeof(ProgressTemplete), new PropertyMetadata(0.001));




        public double ProgressValue
        {
            get { return (double)GetValue(ProgressValueProperty); }
            set {
                SetValue(ProgressValueProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for ProgressValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressValueProperty =
            DependencyProperty.Register("ProgressValue", typeof(double), typeof(ProgressTemplete), new PropertyMetadata(0.0001));



        public string Md5
        {
            get { return (string)GetValue(Md5Property); }
            set { SetValue(Md5Property, value); }
        }

        // Using a DependencyProperty as the backing store for Md5.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Md5Property =
            DependencyProperty.Register("Md5", typeof(string), typeof(ProgressTemplete), new PropertyMetadata(""));



        public static double GetSpeed(DependencyObject obj)
        {
            return (double)obj.GetValue(SpeedProperty);
        }

        public static void SetSpeed(DependencyObject obj, double value)
        {
            obj.SetValue(SpeedProperty, value);
        }

        // Using a DependencyProperty as the backing store for Speed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpeedProperty =
            DependencyProperty.RegisterAttached("Speed", typeof(double), typeof(ProgressTemplete), new PropertyMetadata(0.01));


        public static IntPtr GetTaskIntPtr(DependencyObject obj)
        {
            return (IntPtr)obj.GetValue(TaskIntPtrProperty);
        }

        public static void SetTaskIntPtr(DependencyObject obj, IntPtr value)
        {
            obj.SetValue(TaskIntPtrProperty, value);
        }


        // Using a DependencyProperty as the backing store for TaskIntPtr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TaskIntPtrProperty =
            DependencyProperty.RegisterAttached("TaskIntPtr", typeof(IntPtr), typeof(ProgressTemplete), new PropertyMetadata(IntPtr.Zero));
        
        public static string GetSpeedUnit(DependencyObject obj)
        {
            return (string)obj.GetValue(SpeedUnitProperty);
        }

        public static void SetSpeedUnit(DependencyObject obj, string value)
        {
            obj.SetValue(SpeedUnitProperty, value);
        }

        // Using a DependencyProperty as the backing store for SpeedUnit.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpeedUnitProperty =
            DependencyProperty.RegisterAttached("SpeedUnit", typeof(string), typeof(ProgressTemplete), new PropertyMetadata(""));

        public static Int64 GetStatus(DependencyObject obj)
        {
            return (Int64)obj.GetValue(StatusProperty);
        }

        public static void SetStatus(DependencyObject obj, Int64 value)
        {
            obj.SetValue(StatusProperty, value);
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.RegisterAttached("Status", typeof(Int64), typeof(ProgressTemplete), new PropertyMetadata(Int64.MinValue));
        

        #endregion

        #region 事件

        //暂停/继续事件
        private void Pause_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IntPtr taskIntPtr = GetTaskIntPtr(this);
            DownloadFile item = AppConstant.DownloadProgressList.FirstOrDefault(x => x.TaskIntPtr == taskIntPtr);

            DGIS.Control.DownloadControl downloadControl = new Control.DownloadControl();
            bool downloading = (item.Status == (int)DownloadFile.DownloadStatus.Downloading);
            bool result = false;
            if (downloading)
            {
                result = downloadControl.Stop(taskIntPtr);
                if (result)
                    item.Status = (int)DownloadFile.DownloadStatus.Stop;
            }
            else
            {
                result = downloadControl.Start(taskIntPtr);
                if (result)
                    item.Status = (int)DownloadFile.DownloadStatus.Downloading;
            }
        }

        //删除任务事件
        private void Del_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MessageBox.Show("是否确认删除任务?", "提示", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                return;

            IntPtr taskIntPtr = GetTaskIntPtr(this);
            DownloadFile item = AppConstant.DownloadProgressList.FirstOrDefault(x => x.TaskIntPtr == taskIntPtr);

            DGIS.Control.DownloadControl downloadControl = new Control.DownloadControl();
            if (downloadControl.Del(item)) { 
                for (int i = 0; i < AppConstant.DownloadProgressList.Count; i++)
                {
                    if (AppConstant.DownloadProgressList[i].TaskIntPtr == taskIntPtr)
                    {
                        AppConstant.DownloadProgressList.RemoveAt(i);
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show("删除任务失败");
            }
        }

        //打开文件夹事件
        private void OpenFolder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IntPtr taskIntPtr = GetTaskIntPtr(this);
            DownloadFile item = AppConstant.DownloadProgressList.FirstOrDefault(x => x.TaskIntPtr == taskIntPtr);
            string filePath = string.Format("{0}Cache\\{1}", AppDomain.CurrentDomain.BaseDirectory, item.FileName);

            System.Diagnostics.Process.Start("Explorer.exe", "/select," + filePath);
        }

        #endregion        
    }
}
